


import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;

public class SelectTool extends Tool {
   
  
  protected Point startingMousePosition;
  protected Point currentMousePosition;
  protected Color saveColor;
  protected boolean moving = false;
  protected boolean resizingTop = false;
  protected boolean resizingBottom = false;
  public Point startMove = null;
  public Point endMove = null;

    public SelectTool(DrawingCanvas canvas, LinkedList<ShapeObject> ShapeObjects, MDUser mdUser) {
	  super(canvas, ShapeObjects, mdUser);
	}
  	public ShapeObject getSelectedObject(Point p){
  		for(int i = ShapeObjects.size()-1; i>=0; i--){
			if(((ShapeObject)ShapeObjects.get(i)).contains(startingMousePosition)){
				return (ShapeObject)ShapeObjects.get(i);
			}
		}
  		return null;
  	}

	public void mousePressed(MouseEvent e)  {
		currentMousePosition = startingMousePosition;
		startingMousePosition = e.getPoint();	
		if(sobj != null){
			if(sobj.contains(e.getPoint())){
				if(! resizeCheck(e.getPoint())){
					startMove = e.getPoint();
					moving = true;
				}
			}else{
				sobj = getSelectedObject(startingMousePosition);
				if(sobj != null && sobj.contains(e.getPoint())){
					paintObjects(1);
					sobj.selectObject(canvas);
					if(! resizeCheck(e.getPoint())){
						startMove = e.getPoint();
						moving = true;
					}
				}else{
					paintObjects(1);
					moving = false;
					sobj = null;
				}
			}
		}else{
			sobj = getSelectedObject(startingMousePosition);
			if(sobj!=null){
				paintObjects(1);
				sobj.selectObject(canvas);
			}else{
				paintObjects(1);
			}
		}
	}

	public void mouseDragged(MouseEvent e)  {
		if(sobj != null){
			Point newMousePosition = e.getPoint();
			Graphics iBGraphics = canvas.getImageBufferGraphics();
			paintObjects(1);
			sobj.selectObject(canvas);
			Color c = iBGraphics.getColor();
			iBGraphics.setColor(Color.lightGray);
			if(moving){
			    iBGraphics.drawRect(sobj.startingMousePosition.x+e.getPoint().x-startMove.x,
			    		sobj.startingMousePosition.y+e.getPoint().y-startMove.y, 
			    		sobj.endingMousePosition.x-sobj.startingMousePosition.x, 
			    		sobj.endingMousePosition.y-sobj.startingMousePosition.y);
			    
			}else if(resizingTop){
				iBGraphics.drawRect(e.getPoint().x,
			    		e.getPoint().y, 
			    		sobj.endingMousePosition.x-e.getPoint().x, 
			    		sobj.endingMousePosition.y-e.getPoint().y);
			}else if(resizingBottom){
				iBGraphics.drawRect(sobj.startingMousePosition.x,
			    		sobj.startingMousePosition.y, 
			    		e.getPoint().x-sobj.startingMousePosition.x, 
			    		e.getPoint().y-sobj.startingMousePosition.y);
			}
			iBGraphics.setColor(c);
		}
	}

	public void mouseReleased(MouseEvent e) { 
		if(moving){
			endMove = e.getPoint();
			int dispX = endMove.x - startMove.x;
			int dispY = endMove.y - startMove.y;
			Point p = new Point(sobj.startingMousePosition.x + dispX,
					sobj.startingMousePosition.y + dispY);
			sobj.moveObject(p);
			moving = false;
		}else if(resizingBottom){
			endMove = e.getPoint();
			if(endMove.x>sobj.startingMousePosition.x && 
					endMove.y > sobj.startingMousePosition.y){
				sobj.resizeEndObject(endMove);
			}
			resizingBottom = false;
		}else if(resizingTop){
			endMove = e.getPoint();
			if(endMove.x<sobj.endingMousePosition.x && 
					endMove.y < sobj.endingMousePosition.y){
				sobj.resizeStartObject(endMove);
			}
			resizingTop = false;
		}
		int i = ShapeObjects.indexOf(sobj);
		System.out.println(i);
		paintObjects(0);
		if(i != -1){
			sobj = (ShapeObject)(ShapeObjects.get(i));
			sobj.selectObject(canvas);
		}
	}

	public void keyPressed(KeyEvent e) {}
	
	public boolean resizeCheck(Point p){
		if(sobj.resizeTopArea(p)){
			startMove = p;
			resizingTop = true;
			return true;
		}else if(sobj.resizeBottomArea(p)){
			startMove = p;
			resizingBottom = true;
			return true;
		}else{
			return false;
		}
	}

}
